package com.google.code.p.paintboard.proportions;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.text.DecimalFormat;

import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;


@SuppressWarnings("serial")
public class CustomSpinner<N> extends JSpinner {
	N initial;
	N minimum;
	N maximum;
	N step;
	
	
	
	public CustomSpinner() {
		setModel(new SpinnerNumberModel(new Integer(10), null, null, new Integer(1)));
		addIntegerListeners();
	}

	private void addIntegerListeners() {
		addMouseWheelListener(new MouseWheelListener() {
			public void mouseWheelMoved(MouseWheelEvent e) {
				setValue(Integer.parseInt(getValue().toString())-e.getWheelRotation());
			}
		});
	}
	
	private void addDoubleListeners() {
		addMouseWheelListener(new MouseWheelListener() {
			public void mouseWheelMoved(MouseWheelEvent e) {
				setValue(Double.parseDouble(getValue().toString())-e.getWheelRotation());
			}
		});
	}
	
	public CustomSpinner(int value, int minimum, int maximum, int stepSize) {		
		setModel(new SpinnerNumberModel(value, minimum, maximum, stepSize));
		addIntegerListeners();
	}
	
	public CustomSpinner(double value, double minimum, double maximum, double stepSize) {
		System.out.println("double initial "+value);
		setModel(new SpinnerNumberModel(value, minimum, maximum, stepSize));
		JSpinner.NumberEditor editor = (JSpinner.NumberEditor) getEditor();  
        DecimalFormat format = editor.getFormat();  
        format.setMinimumFractionDigits(2);  
//        editor.getTextField().setHorizontalAlignment(SwingConstants.CENTER);  
        addDoubleListeners();
	}
	
	public int getInteger() {
		return Integer.parseInt(getValue().toString());
	}
	
	public double getDouble() {
		return Double.parseDouble(getValue().toString());
	}
	
	@Override
	public void setValue(Object value) {
//		int integerValue = Integer.parseInt(value.toString());
//		value = (integerValue < 0) ? 0 : integerValue;
		super.setValue(value);
	}
}
